﻿package com.org.system.utils
{
    public class TimeUtil extends Object
    {
		public static const HOUR_TAG:String = '{!hour}';
		public static const MINUTE_TAG:String = '{!minute}';
		public static const SECOND_TAG:String = '{!second}';
		public static const DEFAULT_SECOND_FORMAT:String = HOUR_TAG + ":" + MINUTE_TAG + ":" + SECOND_TAG;

		public static function formatSecond(second:uint, format:String=null) : String
		{
			if (format == null) {
				format = DEFAULT_SECOND_FORMAT;
			}
			
			var hour:uint = Math.floor(second / 3600);
			var minute:uint = Math.floor((second - hour * 3600) / 60);
			var second:uint = second % 60;

			var pairs:Object = {};
			pairs[HOUR_TAG] = StringUtil.sprintf("%.2d", hour);
			pairs[MINUTE_TAG] = StringUtil.sprintf("%.2d", minute);
			pairs[SECOND_TAG] = StringUtil.sprintf("%.2d", second);
			return StringUtil.replace(format, pairs);
		}
		
		public static function formatSecondByRange(second:uint, pairs:Object) : String
		{
			var keys:Array = ObjectUtil.getKeys(pairs);
			keys.sort(Array.NUMERIC);
			for each (var key:uint in keys) {
				if (second < key) {
					if (pairs[key] is Function) {
						return pairs[key](second);
					}
					else {
						return pairs[key];
					}
				}
			}
			return formatSecond(second);
		}
		
		public static function formatTimestamp(timestamp:uint, format:String) : String
		{
			throw new Error("method not implement");
		}		
    }
}
